package com.bl.commons.auth;

import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author bl
 *  跨域支持 OPTIONS请求配置 :options请求是一种预检请求 主要作用是检查服务器和客户端对跨域资源的访问是否符合安全策略
 *  OPTIONS请求解释：https://www.python100.com/html/92144.html
 *
 *  对于ajax的跨域请求，浏览器在发送真实请求之前，会向服务端发送OPTIONS请求，
 *  看服务端是否支持。对于options请求我们只需要返回header，不
 *  需要再进其它的filter，所以加了一个OptionsRequestFilter，
 *  填充header后就直接返回：
*/


/*@Component
public class OptionsRequestFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
        if(request.getMethod().equals("OPTIONS")) {
            response.setHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS,HEAD");
            response.setHeader("Access-Control-Allow-Headers", response.getHeader("Access-Control-Request-Headers"));
            return;
        }
        filterChain.doFilter(request, response);
    }

}*/
